<?php

if(isset($_POST['imie']) && isset($_POST['nazwisko']) && isset($_POST['email'])) {

	if(preg_match('/^[A-ZŻŹĆŃĄŚŁĘÓ][a-zżźćńąśłęó]+$/', $_POST['imie']) == 0) header('Location: ../?errno=201');
	if(preg_match('/^[A-ZŻŹĆŃĄŚŁĘÓ][a-zżźćńąśłęó]+$/', $_POST['nazwisko']) == 0) header('Location: ../?errno=201');
	if(preg_match('/^[a-z0-9][a-z0-9\.\+\-_]*[a-z0-9]@[a-z0-9][a-z0-9\.\-_]*[a-z0-9]\.[a-z][a-z]+$/', $_POST['email']) == 0) header('Location: ../?errno=201');
	if((preg_match('/^[0-9]+$/', $_POST['telefon']) == 0) || (strlen($_POST['telefon']) != 11)) header('Location: ../?errno=201');
	
	include('../sql/sql.php');
	include('funkcje.php');
	
	$mysqli = new mysqli($MySQLSerwer, $MySQLUzytkownik, $MySQLHaslo, $MySQLBaza);

	$stmt = $mysqli->stmt_init();
	
	$stmt->prepare("SELECT COUNT(email) FROM uzytkownicy WHERE email=?");
	$stmt->bind_param('s', $_POST['email']);
	$stmt->execute();
	$stmt->bind_result($czyEmail);
	$stmt->fetch();
	
	if($czyEmail != 0) header('Location: ../?errno=2012');
	
	$stmt->prepare("INSERT INTO uzytkownicy(id_uzytkownika, email) VALUES(NULL, ?)");
	$stmt->bind_param('s', $_POST['email']);
	$stmt->execute();;
	
	$stmt->free_result();
	
	$stmt->prepare("SELECT id_uzytkownika FROM uzytkownicy WHERE email=?");
	$stmt->bind_param('s', $_POST['email']);
	$stmt->execute();
	$stmt->bind_result($idUzytkownika);
	$stmt->fetch();
	
	$stmt->prepare("INSERT INTO klienci(id_klienta, imie, nazwisko, telefon) VALUES(?, ?, ?, ?)");
	$stmt->bind_param('ssss', $idUzytkownika, $_POST['imie'], $_POST['nazwisko'], $_POST['telefon']);
	$stmt->execute();
	
	$plik = $_FILES['zdjecie']['tmp_name'];
	$nazwa = $_FILES['zdjecie']['name'];
	$rozmiar = $_FILES['zdjecie']['size'];
	$typ = $_FILES['zdjecie']['type'];
	
	if($rozmiar <= 10*1024*2024 && eregi("image", $typ)) {
		
		move_uploaded_file($plik, '../photos/'.$idUzytkownika.'.jpg');
		skalujObrazek('../photos/'.$idUzytkownika.'.jpg', 192);
		
	} else {
		
		$err = '&errno=2013';
		
	}
		
	header('Location: ../?infno=201'.$err);
	
} else {

	header('Location: ../?errno=201');

}

?>